package org.fat.nred.actd.tenant.utils;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;

/**
 * <p>sql工具</p>
 *
 * @author TanXin
 * @since 2022/2/17 18:18
 */
public final class SqlUtils {

    private SqlUtils() {

    }

    public static void main(String[] args) throws JSQLParserException {
        String sql = "select * from table_a a";

        Statement statement = CCJSqlParserUtil.parse(sql);
        Select select = (Select) statement;
        System.out.println(select);
        SelectBody selectBody = select.getSelectBody();
        PlainSelect plainSelect = (PlainSelect) selectBody;
        //From分析
        FromItem fromItem = plainSelect.getFromItem();
        Table table = (Table) fromItem; // table.getAlias().getName(), table.getName()
        String name = table.getName();
        System.out.println(name);
        table.setName("table_a_2022");
        System.out.println(select.toString());

    }
}
